﻿/*
Copyright 2010 Andy Uhl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ListMaker.App.Properties;
using System.Collections;

namespace ListMaker.App
{
	public static class PresetHelper
	{
		static PresetHelper()
		{
			VerifyDefaultPresets();
		}

		private static void VerifyDefaultPresets()
		{
			if (Settings.Default.PresetList == null)
			{
				Settings.Default.PresetList = new SerializableDictionary<string, Preset>();
				Settings.Default.Save();
			}

			if (Settings.Default.PresetList.Count == 0)
			{
				Preset sqlInNumeric = new Preset();
				sqlInNumeric.Name = "SQL IN (numeric)";
				sqlInNumeric.Postfix = string.Empty;
				sqlInNumeric.Prefix = string.Empty;
				sqlInNumeric.Separator = ",";
				sqlInNumeric.SingleLine = true;
				sqlInNumeric.RemoveNull = true;
				sqlInNumeric.TrimWhiteSpace = true;
				sqlInNumeric.InputSplitOption = InputSplitOptions.NewLine;

				SavePreset(sqlInNumeric);

				Preset sqlInText = new Preset();
				sqlInText.Name = "SQL IN (text)";
				sqlInText.Postfix = "'";
				sqlInText.Prefix = "'";
				sqlInText.Separator = ",";
				sqlInText.SingleLine = true;
				sqlInText.RemoveNull = true;
				sqlInText.TrimWhiteSpace = true;
				sqlInNumeric.InputSplitOption = InputSplitOptions.NewLine;

				SavePreset(sqlInText);
			}
		}

		public static IDictionary<string, Preset> LoadPresets()
		{
			return (Settings.Default.PresetList as IDictionary<string, Preset>);
		}

		public static void SavePreset(Preset preset)
		{
			if (preset == null)
				return;

			if (Settings.Default.PresetList.ContainsKey(preset.Name))
			{
				Settings.Default.PresetList[preset.Name] = preset;
			}
			else
			{
				Settings.Default.PresetList.Add(preset.Name, preset);
			}

			Settings.Default.Save();
		}

		public static void DeletePreset(Preset preset)
		{
			if (preset == null)
				return;

			if (Settings.Default.PresetList.ContainsKey(preset.Name))
			{
				Settings.Default.PresetList.Remove(preset.Name);
				Settings.Default.Save();
			}
		}

		public static void RenamePreset(Preset preset, string newName)
		{
			if (preset == null)
				return;

			DeletePreset(preset);
			preset.Name = newName;
			SavePreset(preset);
		}
	}
}
